package controllers

import (
	"github.com/astaxie/beego/orm"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/util/gconv"
	"mdblog/models"
	"mdblog/models/blog"
	"strings"
)

type BlogController struct {
	BaseController
}

func (o *BlogController) Prepare() {
	o.Data["Configs"], _ = models.GetAllConfigs()
	if user := o.GetSession("user"); user != nil {
		o.Data["User"] = user
	}
}

func (o *BlogController) Login() {
	o.TplName = "blog/login.html"
}

func (o *BlogController) Index() {
	page, _ := o.GetInt("page")
	if page == 0 {
		page = 1
	}
	Configs, _ := models.GetAllConfigs()
	articles, total, err := blog.GetArticlesPaginator(gconv.Int(Configs["PageSize"]), page)
	if err != nil {
		o.Abort("500")
	}
	classify, err := blog.GetAllBlogClassify()
	if err != nil {
		o.Abort("500")
	}

	notice, err := blog.GetDefaultNotice()
	if err != nil {
		o.Abort("500")
	}

	o.Data["Notice"] = notice
	o.Data["AllClassify"] = classify
	o.Data["Articles"] = articles
	o.Data["Curr"] = page
	o.Data["PageCount"] = total
	o.Layout = "blog/base/layout.html"
	o.LayoutSections = make(map[string]string)
	o.LayoutSections["HtmlHead"] = "blog/blog/detail/header.html"
	o.TplName = "blog/blog/index/index.html"
}

func (o *BlogController) Article() {
	classify, err := blog.GetAllBlogClassify()
	if err != nil {
		o.Abort("500")
	}

	id, err := o.GetInt(":id")
	if err != nil {
		o.Abort("404")
		return
	}
	article, err := blog.GetBlogArticle(id)
	if err != nil {
		o.Abort("404")
		return
	}

	address := o.Ctx.Input.IP()
	if v := o.Ctx.Input.Header("X-Read-IP"); v != "" {
		address = v
	}
	article.AddRead(address)
	o.Data["AllClassify"] = classify
	o.Layout = "blog/base/layout.html"
	o.LayoutSections = make(map[string]string)
	o.LayoutSections["HtmlHead"] = "blog/blog/detail/header.html"
	o.LayoutSections["Scripts"] = "blog/blog/detail/scripts.html"
	o.Data["Classify"], _ = blog.GetBlogClassify(article.Classify)
	o.Data["Article"] = article
	o.TplName = "blog/blog/detail/detail.html"
}

func (o *BlogController) ClassifyArticles() {
	classifyKey := o.GetString(":classify")
	classify, err := blog.GetBlogClassifyFromKey(classifyKey)
	if err != nil {
		if err == orm.ErrNoRows {
			o.Abort("404")
		} else {
			glog.Error(err.Error())
			o.Abort("500")
		}
	}
	page, _ := o.GetInt("page")
	if page == 0 {
		page = 1
	}

	Configs, _ := models.GetAllConfigs()
	articles, total, err := blog.GetBlogArticlesByClassify(&classify, gconv.Int(Configs["PageSize"]), page)

	if err != nil {
		o.Abort("500")
	}

	targets, err := blog.GetClassifyTarget(classify)
	if err != nil {
		glog.Error(err.Error())
		o.Abort("500")
	}

	notice, err := blog.GetDefaultNotice()
	if err != nil {
		o.Abort("500")
	}

	o.Data["Notice"] = notice
	o.Data["AllTargets"] = targets
	o.Data["Articles"] = articles
	o.Data["Curr"] = page
	o.Data["PageCount"] = total

	o.Layout = "blog/base/classify_layout.html"
	o.LayoutSections = make(map[string]string)
	o.LayoutSections["HtmlHead"] = "blog/blog/index/header.html"
	o.TplName = "blog/blog/index/index.html"
}

func (o *BlogController) Resource() {
	md5 := o.GetString(":name")
	if md5 == "" {
		glog.Debug(1)
		o.Abort("404")
	}

	resource, err := blog.GetResourceFromMd5(md5)
	if err != nil {
		if err == orm.ErrNoRows {
			glog.Debug(2)
			o.Abort("404")
		}
		glog.Errorf("%s", err)
		o.Abort("500")
	}

	switch strings.ToUpper(resource.Ext) {
	case ".JPG":
		o.Download(resource.Path, "image/jpg")
	case ".PNG":
		o.Download(resource.Path, "image/png")
	case ".MP4":
		o.Ctx.Output.Header("Accept-Ranges", "bytes")
		o.Ctx.Output.Header("Content-Length", gconv.String(resource.Size))
		o.Download(resource.Path, "video/mp4")

	default:
		o.Ctx.Output.Download(resource.Path, resource.Name)
	}

}
